<template>
  <!-- 高维键 -->
  <div class='container'>
    <el-card>
      <!-- 表单 -->
      <el-row>
        <el-form ref="randomsFormRef" :model="randomsForm" label-width="80px">
          <el-col :span="6">
            <el-form-item label="关键字" prop="keyword">
              <el-input placeholder="根据编号搜索" v-model="randomsForm.keyword" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-row type='flex' justify="end">
              <el-button size="small" @click="resetForm">清除</el-button>
              <el-button type="primary" size="small" @click="searchData">搜索</el-button>
            </el-row>
          </el-col>
        </el-form>
      </el-row>
      <!-- 警告框 -->
      <el-alert :title="'数据一共'+counts+'条'" :closable="false" type="info" show-icon>
      </el-alert>
      <!-- 表格 -->
      <el-table :data="randomsList" style="width: 100%">
        <el-table-column prop="id" label="编号" width="220">
        </el-table-column>
        <el-table-column prop="questionType" label="题型" width="80">
          <template v-slot='{row}'>
            <span v-if="row.questionType==='1'">单选</span>
            <span v-else-if="row.questionType==='2'">多选</span>
            <span v-else-if=" row.questionType==='3'">简答</span>
          </template>
        </el-table-column>
        <el-table-column prop="questionIDs" label="题目编号" width="220">
          <template v-slot='{row}'>
            <el-button class="aaa" type="text" v-for="(item,i) in row.questionIDs" :key="i" @click="preview(item)">
              {{item.number}}<br /></el-button>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="180">
        </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)">
        </el-table-column>
        <el-table-column prop="userName" label="录入人">
        </el-table-column>
        <el-table-column label="操作" width="80">
          <template v-slot='{row}'>
            <el-button type="danger" icon="el-icon-delete" circle plain @click="deleteData(row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination style="float:right;margin:10px 0" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="randomsForm.page" :page-sizes="[20,30,50,100]"
        :page-size="randomsForm.pagesize" background :total="counts" layout=" prev, pager, next,sizes, jumper">
      </el-pagination>

    </el-card>

    <Preview ref="previewDialogRef" :previewDialogVisibleShow.sync="previewDialogVisibleShow"></Preview>

  </div>
</template>

<script>
import Preview from '../components/questions-preview.vue'
import { randoms, removeRandoms } from '@/api/hmmm/questions'
export default {
  data () {
    return {
      randomsForm: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      counts: 0,
      randomsList: [],
      previewDialogVisibleShow: false
    }
  },
  methods: {
    // 获取数据列表
    async getRandomsList (val) {
      const { data: res } = await randoms(val)
      console.log(res)
      this.counts = res.counts
      this.randomsList = res.items
      console.log(this.randomsList)
    },
    // 清空搜索条件
    resetForm () {
      this.$refs.randomsFormRef.resetFields()
    },
    // 搜索
    searchData () {
      this.randomsForm.page = 1
      this.getRandomsList(this.randomsForm)
    },
    // 删除数据
    async deleteData (val) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await removeRandoms(val)
          // this.getRandomsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } catch (error) {
          this.$message.error('删除失败')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log('1')
      // this.getRandomsList()
      this.handleCurrentChange()
    },
    // 监听每页数据条数
    handleSizeChange (newsize) {
      this.randomsForm.pagesize = newsize
      this.getRandomsList(this.randomsForm)
    },
    // 监听当前页数
    handleCurrentChange (newpage) {
      this.randomsForm.page = newpage
      this.getRandomsList(this.randomsForm)
    },
    // 预览组件
    preview (row) {
      this.$refs.previewDialogRef.loadingPreviewList(row.id)
      this.previewDialogVisibleShow = true
    }

  },
  created () {
    this.getRandomsList()
  },
  components: {
    Preview
  }
}
</script>

<style scoped lang='less'>
.el-card {
  margin: 10px;
}
.el-alert {
  margin-bottom: 10px;
}
.aaa {
  margin: 0;
  padding: 0;
}
</style>
